package com.victor.utils.sms;

import java.util.Date;
import java.util.TreeMap;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.victor.entity.sms.RetSms;

/**
 * 发送短息 方式
 * 
 * 模板id 
 * 验证码模板 --11301-- 10分钟 
 * 密码找回 --11302-- 10分钟 
 * 密码更改 --11303-- 10分钟 
 * 银行卡验证 --11304-- 5分钟 
 * 实名认证 --11305-- 5分钟 
 * 会员短信模板 --11306 -- 无时间
 * 
 * 注：当模板id为 11306 时 code传空字符串
 */
public class SmsSend {
	private static Logger log = LoggerFactory.getLogger(SmsSend.class);

	private static String KEY = "fa604ba76d4dad3c";

	public static void main(String[] args) {

		String mobile = "18704074221";
//		String mobile = "18624013864";
		System.out.println(smsSendMessage(mobile, "11301", "123456"));
		System.out.println(smsSendMessage(mobile, "11302", "123456"));
		System.out.println(smsSendMessage(mobile, "11303", "123456"));
		System.out.println(smsSendMessage(mobile, "11304", "123456"));
		System.out.println(smsSendMessage(mobile, "11305", "123456"));
		System.out.println(smsSendMessage(mobile, "11323", ""));

	}

	/**
	 * 发送短信
	 */
	public static Integer smsSendMessage(String phone, String templateId, String code) {

		if (phone == null || phone.equals("")) {// 如果传入手机号为空
			return 0;
		}
		if (templateId == null || templateId.equals("")) {// 如果传入模板id为空
			return 0;
		}

		RetSms retSms = new RetSms();
		try {
			TreeMap<String, Object> map = new TreeMap<String, Object>();
			map.put("cpid", 2907);
			map.put("templateId", templateId);
			map.put("mobile", phone);
			if (!templateId.equals("11323")) {// 如果是会员短信模板 则不需要code
				map.put("parameter", "{code:" + code + "}");
			}
			map.put("timestamp", new Date().getTime());

			String _sign = SmsUseful.createSign(map);
			String sign = SmsUseful.getKeyedDigestGBK(_sign, KEY);
			map.put("verify", sign);

			String resource = "http://api.hstang.net/service/sms/send";

			CloseableHttpClient httpClient = HttpClientBuilder.create().build();

			String body = JSON.toJSONString(map);

			System.out.println(body);
			HttpPost post = new HttpPost(resource);
			StringEntity se = new StringEntity(body, "UTF-8");
			se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
			post.addHeader("Content-Type", "application/json;charset=UTF-8");
			post.addHeader("accept", "application/json");
			post.setEntity(se);
			HttpResponse httpResponse = httpClient.execute(post);

			String result = IOUtils.toString(httpResponse.getEntity().getContent());
			log.info(result);

			retSms = JSONObject.parseObject(result, RetSms.class);

		} catch (Exception e) {
			e.printStackTrace();
		}
		if (retSms.getStatus() == 0) {
			return 1;
		}
		return 0;
	}

}
